package com.test21_常用类;

import org.junit.Test;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

/**
 * 大数字处理类
 */
public class Demo09_BigDecimal {

    @Test
    public void test1() {
        // BigInteger
        BigInteger bigA = new BigInteger("2378902389023");
        BigInteger bigB = new BigInteger("723782378");
        System.out.println("【加法计算】" + bigA.add(bigB));
        System.out.println("【减法计算】" + bigA.subtract(bigB));
        System.out.println("【乘法计算】" + bigA.multiply(bigB));
        System.out.println("【除法计算】" + bigA.divide(bigB));
        // 计算商和余数
        BigInteger[] result = bigA.divideAndRemainder(bigB);
        System.out.println("【除法计算】商：" + result[0] + "、余数：" + result[1]);
    }

    @Test
    public void test2() {
        // BigDecimal
        BigDecimal bigA = new BigDecimal("2378902389023.232323231");
        BigDecimal bigB = new BigDecimal("723782378.7237238");
        System.out.println("【加法计算】" + bigA.add(bigB));
        System.out.println("【减法计算】" + bigA.subtract(bigB));
        System.out.println("【乘法计算】" + bigA.multiply(bigB));
        System.out.println("【除法计算】" + bigA.divide(bigB, RoundingMode.HALF_UP));
    }

    @Test
    public void test3() {
        // 通过BigDecimal实现四舍五入
        System.out.println(MathUtil.round2(19.56789, 2));
    }
}
